Basic Mathematimatical Operations

Python is well suited to perform basic arithmetic and is well supplemented by the numpy and scipy libraries.

By the end of this file you should have seen simple examples of:

  1. Basic arythmetic
  2. Powers and roots
  3. Logarithmic functions
  4. Trigonometric functions

Further Reading:
https://docs.python.org/2/library/math.html#power-and-logarithmic-functions
https://docs.python.org/2/library/math.html#trigonometric-functions


In [1]:
# Python Imports
import math

In [2]:
print('The sum of 3 and 4 is: {0}'.format(3 + 4))
print('The product of 3 and 4 is: {0}'.format(3 * 4))
print('The difference of 3 with 4 is: {0}'.format(3 - 4))
print('The dividend of 3 with 4 is: {0}'.format( 3 / 4))
print('The modulo of 3 with 4 is: {0}'.format(3 % 4))


The sum of 3 and 4 is: 7
The product of 3 and 4 is: 12
The difference of 3 with 4 is: -1
The dividend of 3 with 4 is: 0.75
The modulo of 3 with 4 is: 3

In [3]:
# Powers and roots
print('The power of 3 to the 4 is: {0}'.format(3 ** 4))
print('The 3rd root of 4 is: {0}'.format(4 ** (1.0 / 3)) )


The power of 3 to the 4 is: 81
The 3rd root of 4 is: 1.5874010519681994

In [4]:
#Logarithmic functions
print('The log base 10 of 3 is: {0}'.format(math.log10(3)) )
print('The log base e of 3 is: {0}'.format(math.log(3)) )
print('The log base 3 of 4 is: {0}'.format(math.log(4,3)) )


The log base 10 of 3 is: 0.47712125471966244
The log base e of 3 is: 1.0986122886681098
The log base 3 of 4 is: 1.2618595071429148

In [5]:
# Trigonometric functions
print('The sine of pi is: {0}'.format(math.sin(math.pi)) )
print('The sine of pi is: {0}'.format(math.cos(math.pi)) )
print('The arc tangent of pi is: {0}'.format(math.atan(math.pi)) )


The sine of pi is: 1.2246467991473532e-16
The sine of pi is: -1.0
The arc tangent of pi is: 1.2626272556789115